package com.reserv.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.reserv.dao.ServiceDAO;
import com.reserv.util.SearchVar;
import com.reserv.util.TimestampUtil;
import com.reserv.vo.ServiceVO;

public class ReservListAction implements CommandProcess {
	private static final int PAGE_SIZE = 5;
	private static final int PAGE_GROUP = 10;

	public ForwardAction requestProcess(
			HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {		
		request.setCharacterEncoding("utf-8");
		
		String pageNum = request.getParameter("pageNum");
		String type = request.getParameter("type");
		
		String kind = request.getParameter("kind");
		String local = request.getParameter("local");
		
		TimestampUtil tsu = new TimestampUtil();
		Timestamp rtsDate = tsu.toTimestamp(request.getParameter("rtsDate"));
		Timestamp rteDate = tsu.toTimestamp(request.getParameter("rteDate"));
		Timestamp rsDate = tsu.toTimestamp(request.getParameter("rsDate"));
		Timestamp reDate = tsu.toTimestamp(request.getParameter("reDate"));
		
		System.out.println("종류 : " + kind + ", 지역 : " + local);
		System.out.println("접수기간 : " + rtsDate + " ~ " + rteDate);
		System.out.println("이용기간 : " + rsDate + " ~ " + reDate);
		
		SearchVar sv = new SearchVar(kind, local, rtsDate, rteDate, rsDate, reDate);
		
		request.setAttribute("kind", sv.getKind());
		request.setAttribute("local", sv.getLocal());
		request.setAttribute("rtsDate", sv.getRtsDate());
		request.setAttribute("rteDate", sv.getRteDate());
		request.setAttribute("rsDate", sv.getRsDate());
		request.setAttribute("reDate", sv.getReDate());
		
		if(type == null) {
			ForwardAction forward = new ForwardAction();
			forward.setRedirect(false);
			forward.setPath("/main/index.jsp?body=/error/error.jsp");
			return forward;
		}
		
		if(pageNum == null) {
			pageNum = "1";
		}
		int currentPage = Integer.valueOf(pageNum);

		int startRow = currentPage * PAGE_SIZE - PAGE_SIZE;

		ServiceDAO dao = ServiceDAO.getInstance();
		int listCount = dao.getServiceCount(type, sv);
		
		System.out.println("listCount" + listCount);
		
		ArrayList<String> sKindList = null;
		ArrayList<String> sLocalList = null;
		sKindList = (ArrayList<String>) dao.getKindList();
		request.setAttribute("sKindList", sKindList);
		sLocalList = (ArrayList<String>) dao.getLocalList();
		request.setAttribute("sLocalList", sLocalList);
		
		ArrayList<ServiceVO> serviceList = null;
		if(listCount > 0) { 
			serviceList = (ArrayList<ServiceVO>) dao.getListVOs(type, startRow, PAGE_SIZE, sv);

			int pageCount = 
					listCount / PAGE_SIZE + (listCount % PAGE_SIZE == 0 ? 0 : 1);

			int startPage = (currentPage / PAGE_GROUP) * PAGE_GROUP + 1
					- (currentPage % PAGE_GROUP == 0 ? PAGE_GROUP : 0);

			int endPage = startPage + PAGE_GROUP - 1;

			if(endPage > pageCount) {
				endPage = pageCount;
			}
			
			
			request.setAttribute("serviceList", serviceList);
			request.setAttribute("pageCount", pageCount);
			request.setAttribute("startPage", startPage);
			request.setAttribute("endPage", endPage);
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("listCount", listCount);
			request.setAttribute("PAGE_GROUP", PAGE_GROUP);

			request.setAttribute("tsu", tsu);
		}
		ForwardAction forward = new ForwardAction();
		forward.setRedirect(false);
		forward.setPath("/main/index.jsp?body=/reserv/list.jsp?type=" + type);
		return forward;
	}
}
